<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>客服助手Demo</title>
    <style>
        html,body {
            font-family: Arial, sans-serif;
            height: 100%;
            margin: 0; /* 移除默认的边距 */
            padding: 0; /* 移除默认的内边距 */
        }
        #chat-container {
            width: 96%;
            max-width: 2000px;
            height: 98%;
            margin: 0 auto;
            border: 1px solid #ccc;
        }
        #chat-header {
            background-color: #f8f8f8;
            padding: 10px;
            border-bottom: 1px solid #ccc;
        }
        #chat-body {
            height: 70%;
            padding: 10px;
            overflow-y: scroll;
        }
        .chat-message {
            margin-bottom: 10px;
            width: 100%;
            height: 30px;
        }
        .message-bubble {
            max-width: 70%;
            padding: 8px;
            border-radius: 5px;
            display: inline-block;
        }
        .user .message-bubble {
            background-color: #DCF8C6;
            float: right;
        }
        .assistant .message-bubble {
            background-color: #ECECEC;
            float: left;
        }
        #chat-footer {
            border-top: 1px solid #ccc;
            padding: 10px;
        }
        #chat-input {
            width: 80%;
            padding: 10px;
        }
        #send-button {
            width: 18%;
            padding: 10px;
            cursor: pointer;
            background-color: #007BFF; color: #fff; border: none;
            border-radius: 3px; cursor: pointer;
        }
        #send-button-send {
            width: 18%;
            padding: 10px;
            cursor: pointer;
            background-color: #6ab1fc; color: #fff; border: none;
            border-radius: 3px; cursor: pointer;
            display: none;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <div id="chat-container">
        <div id="chat-header">
            <h1>客服助手Demo</h1>
        </div>
        <div id="chat-body">
            <!-- 对话内容 -->
        </div>
        <div id="chat-footer">
            <input type="text" id="chat-input" placeholder="请输入你的问题..." required>
            <button id="send-button">发送</button>
            <button id="send-button-send">正在解析中...</button>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            $('#send-button').click(function() {
                var userInput = $('#chat-input').val();
                if (userInput.trim() !== '') {
                    $('#send-button').hide()
                    $('#send-button-send').show()
                    sendMessage(userInput);
                    $('#chat-input').val('');
                }
            });

            function sendMessage(message) {
                var messagesDiv = $('#chat-body');
                var userBubble = $('<div>').addClass('chat-message user').html('<div class="message-bubble">' + message + '</div>');
                messagesDiv.append(userBubble);
                messagesDiv.scrollTop(messagesDiv.prop("scrollHeight")); // Scroll to the bottom

                // Simulate an API call
                $.ajax({
                    url: 'http://127.0.0.1:8000/chat/demo2',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify({ question: message }),
                    success: function(data) {
                        $('#send-button').show()
                        $('#send-button-send').hide()
                        var assistantBubble = $('<div>').addClass('chat-message assistant').html('<div class="message-bubble">' + data.data.answer + '</div>');
                        messagesDiv.append(assistantBubble);
                        messagesDiv.scrollTop(messagesDiv.prop("scrollHeight")); // Scroll to the bottom
                    },
                    error: function(error) {
                        $('#send-button').show()
                        $('#send-button-send').hide()
                        console.error('Error:', error);
                    }
                });
            }
        });
    </script>
</body>
</html>
